import validate from 'wechat-validate'
Page({
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseList: [],
    houseInfo:"",
    currentDate:Date.now(),
    maxDate:Date.now()+1000*3600*24*3,//最大时间
    // 表单数据
    houseId: "",
    name: "",
    gender: 0,
    mobile: "",
    visitDate: ""
  },
  behaviors: [validate],
  onLoad(){
    this.getHouseList()
  },
  // 获取我的房屋列表
  async getHouseList() {
    let houseList = await wx.http.get('/room')
    // console.log(houseList)
    houseList = houseList.map(item => {
      return {
        name: item.point + item.building + item.room,
        id: item.id
      }
    })
    this.setData({
      houseList
    })
  },
  // 选择房屋
  selectHouseInfo({detail}) {
    // console.log(detail);
    this.setData({
      houseId: detail.id,
      houseInfo: detail.name
    })
  },
  // 时间选择
  selectDateLayer(event) {
    // console.log(event.detail)
    this.setData({
      dateLayerVisible: false,
      visitDate: wx.utils.dataFormat(event.detail)
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
 async goPassport() {
    const flag=this.validate()
    if(!flag) return
    const {houseId,visitDate,name,gender,mobile}=this.data
    let res = await wx.http.post('/visitor',{
      houseId,visitDate,name,gender,mobile
    })
    wx.utils.toast('操作成功')
    setTimeout(()=>{
      wx.reLaunch({
        url: '/visitor_pkg/pages/passport/index?id=' + res.id,
      })
    },500)
  },
})
